<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */
?>
<?php
$this->headStyle($this->APP_STATIC_SERVER . '/js/jquery.autocomplete/jquery.autocomplete.css');

$this->headScript($this->APP_STATIC_SERVER . '/js/jquery.bgiframe/jquery.bgiframe.min.js')
	 ->headScript($this->APP_STATIC_SERVER . '/js/jquery.autocomplete/jquery.autocomplete.js');
?>

<div class="content" style="margin-top: 10px;">
	<div class="box header mb0">
		<div class="head"><div></div></div>
		<h2><?php echo $this->TRANSLATOR->translator('tag_tagger', 'tag'); ?></h2>
	</div>
	
	<div class="desc">
		<div class="body">
			<p class="g_a_bottom">
				<label for="simple_input"><?php echo $this->TRANSLATOR->translator('tag_tagger_enter_tag', 'tag'); ?></label>
				<input type="text" style="width: 95%" id="tagInput" class="text tagInput" />
			</p>
			<?php if ($this->ACL->isAllowed('add', 'tag', 'tag')) : ?>
			<p class="g_a_bottom">
				<label for="simple_input"><?php echo $this->TRANSLATOR->translator('tag_tagger_add_tag', 'tag'); ?></label>
				<input type="text" style="width: 50%; float: left;" id="tag_text" class="text tagInput" name="tag[tag_text]" />
				<a style="float: left; margin-left: 10px;" class="button green" id="tag_tagger_add_button"><small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('tag_tagger_add_button', 'tag'); ?></span></a>
			</p>
			<div class="clear"></div>
			<?php endif; ?>
			<div>
				<div class="g_a_bottom">
					<p style="font-size: 11px;"><?php echo $this->TRANSLATOR->translator('tag_tagger_list_of_selected_tags', 'tag'); ?></p>
					<br />
					
					<div id="tagsList" class="g_a_tags" style="height: 200px; overflow: auto; margin-top: 5px;">
						<?php if (isset($this->tags)) : ?>
						<?php foreach ($this->tags as $tag) : ?>
						<div class="g_a_tag_item"><?php echo $tag['tag_text']; ?><input type="hidden" name="tagIds[]" value="<?php echo $tag['tag_id']; ?>" /></div>
						<?php endforeach; ?>
						
						<div class="clearfix"></div>
						<?php endif; ?>
					</div>
					
					<div class="clearfix"></div>
					
					<input type="hidden" name="tagItemName" value="<?php echo $this->tagItemName; ?>" />
					<input type="hidden" name="tagItemRouteName" value="<?php echo $this->tagItemRouteName; ?>" />
					<input type="hidden" name="tagDetailsRouteName" value="<?php echo $this->tagDetailsRouteName; ?>" />
				</div>
			</div>
		</div>
	</div>
	
	<div class="body_footer">
		<div class="bottom_left"><div class="bottom_right"></div></div>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
	/**
	 * Remove tag handler
	 */
	$('#tagsList').find('span').click(function() {
		$(this).remove();
	});

	/**
	 * Make list tags autocomplete
	 */
	$('#tagInput').autocomplete('<?php echo $this->url('tag_tag_suggest'); ?>', {
		extraParams: { limit: 10 }
	}).result(function(event, item) {
		$('#tagInput').val('');
		if ($('#tagsList').find('input[type="hidden"][value="' + item[1] + '"]').length == 0) {
			$('<div/>')
				.html(item[0])
				.addClass('g_a_tag_item')
				.append($('<input/>').attr('type', 'hidden').attr('name', 'tagIds[]').val(item[1]))
				.click(function() {
					$(this).remove();
				}).appendTo($('#tagsList'));
		}
	});

	$('#tagsList').children('div').each(function() {
		var self = this;
		if ($(self).hasClass('g_a_tag_item')) {
			$(self).click(function() {
				$(self).remove();
			});
		}
	});
	
	<?php if ($this->ACL->isAllowed('add', 'tag', 'tag')) : ?>
	$('#tag_tagger_add_button').bind('click', function() {
		var tagText = $('#tag_text').val();
		if (tagText != null && tagText != '') {
			$.ajaxq('tag_add', {
				url: '<?php echo $this->url('tag_tag_add'); ?>',
				type: 'POST',
				data: { tag_text: tagText, type: 'ajax' },
				success: function(response) {
					var res = $.evalJSON(response);
					if (res.status == 'RESULT_OK') {
						$('#tag_text').val('');
						$('<div/>')
							.html(res.tag_text)
							.addClass('g_a_tag_item')
							.append($('<input/>').attr('type', 'hidden').attr('name', 'tagIds[]').val(res.tag_id))
							.click(function() {
								$(this).remove();
						}).appendTo($('#tagsList'));
						Gio.Message.success('<?php echo $this->TRANSLATOR->translator('tag_tagger_add_tag_success', 'tag'); ?>');
					} else if (res.status == 'RESULT_EXIST') {
						Gio.Message.error('<?php echo $this->TRANSLATOR->translator('tag_tagger_add_tag_exist', 'tag'); ?>');
					} else {
						Gio.Message.error('<?php echo $this->TRANSLATOR->translator('common_error_message', 'core'); ?>');
					}
				}
			});
		}
	});
	<?php endif; ?>
});
</script>